var sysMenu = {
	init : function() {
		
		sysMenu.sysMenuTreegrid=$('#sysMenuTreegrid');
		sysMenu.sysMenuAddDialog=$('#sysMenuAddDialog');
		sysMenu.sysMenuAddFrom=$('#sysMenuAddFrom');
		sysMenu.sysMenuUpdateDialog=$('#sysMenuUpdateDialog');
		sysMenu.sysMenuUpdateFrom=$('#sysMenuUpdateFrom');
		
		sysMenu.showParentMenus=$('#showParentMenus');
		
		sysMenu.text=$('#text');
		sysMenu.updateText=$('#updateText');
		
		sysMenu.sysMenuTreegrid.treegrid({
		    url: window.ctx +'/sys/menu/listMenus',
		    idField:'id',
		    treeField:'text',
		    fixed: true,
		    animate:true,
		    fitColumns: true,
		    rownumbers: true,
		    singleSelect: false,
		    columns:[[
				{field:'id', width:100, checkbox: true},
				{field:'text', title:'菜单名称', width: 100},
				{field:'url', title:'菜单地址', width: 100},
				/*{field:'state', title:'是否父节点', width: 50},
				{field:'enable', title:'是否有效', width: 50},*/
		    ]],
		    onLoadSuccess: function(node, data) {
		    	$(this).treegrid('expandAll')
			},
			onCheck:function(node){
			   if(node.children != null){
	           }
			},
			onUncheck:function(node){
				if(node.children != null){
	                $(node.children).each(function(){
	                	sysMenu.sysMenuTreegrid.treegrid('unselect', this.id);
	                });
	           }
			},
			/*onDblClickCell:function(rowIndex, rowData){
				sysMenu.toolbar.showUpdateDialog(rowData.id);
			}*/
		});
		(function(){
			document.onkeydown = function()  
	        {  
				var alt = event.altKey;
				var keyCode = window.event.keyCode;
	            if (alt && keyCode==65 ) {  
	                sysMenu.toolbar.showAddDialog();
	            } else if(alt && keyCode==67){
	            	sysMenu.toolbar.showUpdateDialog();
	            }else if(alt && keyCode==90){
	            	sysMenu.toolbar.redo();
	            }
	        }  
		})();
	},
	toolbar:{
		showAddDialog: function(id) {
			var getFatherMenu = function(v) {
				sysMenu.sysMenuAddDialog.dialog('open');
				/*$.messager.progress({
					text : '正在处理...'
				});
				$.post(window.ctx + '/SysMenuServlet?command=getFatherMenu', {id : v}, function(data) {
					if (data) {
						sysMenu.sysMenuAddFrom.form('load', {
							id: data.id,
							ptext: data.text
						});
						sysMenu.sysMenuAddDialog.dialog('open');
						sysMenu.text.focus();
					} else {
						$.messager.alert('系统提示', '获取信息失败！', 'error')
					}
					$.messager.progress('close');
				}, 'json');*/
			}
			if(id){
				getFatherMenu(id);
			}else{
				var row = sysMenu.sysMenuTreegrid.datagrid('getSelections');
				if(row.length==1){
					getFatherMenu(row[0].id);
				}else{
					getFatherMenu(0);
				}
				
			}
		},
		
		hiddenAddDialog: function() {
			sysMenu.sysMenuTreegrid.treegrid('clearSelections'); 
			sysMenu.sysMenuAddDialog.dialog('close');
			sysMenu.toolbar.resetAddFrom();
		},
		resetAddFrom: function() {
			sysMenu.sysMenuAddFrom.form('reset');
		},
		add:function () {
			if(sysMenu.sysMenuAddFrom.form('validate')){
				$.messager.progress({
					text : '正在处理...'
				});
				console.info("pid:"+$('.addParentMenu').combobox('getValue'))
				$.post(window.ctx + '/sys/menu/addMenu/'+$('.addParentMenu').combobox('getValue'), sysMenu.sysMenuAddFrom.serialize(), function(data) {
					if (data) {
						$.messager.show({
							title: '系统提示',
							msg: '新增成功！',
							timeout: 3000,
							showType: 'slide'
						});
						sysMenu.toolbar.resetAddFrom();
						sysMenu.sysMenuTreegrid.treegrid('reload');	
					} else {
						$.messager.alert('系统提示', '新增失败！', 'error')
					}
					$.messager.progress('close');
					sysMenu.toolbar.hiddenAddDialog();
				});
			}
		},
		showUpdateDialog:function(id){
			var getSysMenu = function(v) {
				$.post(window.ctx + '/sys/menu/listMenuById', {id : v}, function(datas) {
					if (datas) {
						var data = datas[0];
						$('.updateParentMenu').combobox('setValue',datas[1]);
						if(data.pid == "0"){
							$('#hideParentMenu').hide();
							$('#hideParentURL').hide();
						}else{
							$('#hideParentMenu').show();
							$('#hideParentURL').show();
						}
						sysMenu.sysMenuUpdateFrom.form('load', {
							pid:data.pid,
							id: data.id,
							text:data.text,
							url:data.url,
							state:data.state,
							enable:data.enable
						});
						sysMenu.sysMenuUpdateDialog.dialog('open');
						sysMenu.updateText.focus();
					} else {
						$.messager.alert('系统提示', '获取信息失败！', 'error')
					}
					$.messager.progress('close');
				}, 'json');
			}
			if(id){
				getSysMenu(id);
			}else{
				var row = sysMenu.sysMenuTreegrid.datagrid('getSelections');
				console.info(row)
				if(row.length==1){
					console.info(row[0].id)
					getSysMenu(row[0].id);
				}else{
					$.messager.show({
						title: '系统提示',
						msg: '请选择一行数据进行修改！',
						timeout: 3000,
						showType: 'slide'
					});	
				}
				
			}
		},
		hiddenUpdateDialog: function() {
			sysMenu.sysMenuUpdateDialog.dialog('close');
			sysMenu.sysMenuTreegrid.treegrid('clearSelections'); 
			sysMenu.toolbar.resetUpdateForm();
		},
		resetUpdateForm: function() {
			sysMenu.sysMenuUpdateFrom.form('reset');
		},
		
		update:function () {
			if(sysMenu.sysMenuUpdateFrom.form('validate')){
				$.messager.progress({
					text : '正在处理...'
				});
				$.post(window.ctx + '/sys/menu/updateMenuById', sysMenu.sysMenuUpdateFrom.serialize(), function(data) {
					if (data) {
						$.messager.show({
							title: '系统提示',
							msg: '修改成功！',
							timeout: 3000,
							showType: 'slide'
						});
						sysMenu.toolbar.resetUpdateForm();
						sysMenu.sysMenuTreegrid.treegrid('clearSelections'); 
						sysMenu.sysMenuTreegrid.treegrid('reload');		
					} else {
						$.messager.alert('系统提示', '修改失败！', 'error')
					}
					$.messager.progress('close');
					sysMenu.toolbar.hiddenUpdateDialog();
				});
			}
		},
		
		updateState:function(id){
			layer.confirm('是否确认删除？', {
                icon: 4,
                btn: ['确认','取消']
  			}, function(){
  				var getupdateState = function(v) {
  					$.post(window.ctx + '/sys/menu/deleteMenuById', {id : v}, function(data) {
  						if (data) {
  							layer.closeAll();
  							$.messager.show({
  								title: '系统提示',
  								msg: '删除成功！',
  								timeout: 3000,
  								showType: 'slide'
  							});
  							sysMenu.sysMenuTreegrid.treegrid('clearSelections'); 
  							sysMenu.sysMenuTreegrid.treegrid('reload');	
  						} else {
  							$.messager.alert('系统提示', '获取信息失败！', 'error')
  						}
  						$.messager.progress('close');
  					});
  				}
  				if(id){
  					getupdateState(id);
  				}else{
  					var row = sysMenu.sysMenuTreegrid.datagrid('getSelections');
  					if(row.length==1){
  						getupdateState(row[0].id);
  					}else{
  						$.messager.show({
  							title: '系统提示',
  							msg: '请选择一行数据！',
  							timeout: 3000,
  							showType: 'slide'
  						});
  					}
  					
  				}
  			});
		},
		redo: function() {
			sysMenu.sysMenuTreegrid.datagrid('unselectAll');
		}
	}
}

$(function() {
	sysMenu.init();
});